<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @row-del="rowDel"
        @search-reset="searchReset"
      >
        <template slot-scope="scope" slot="orderTimeSearch">
          <el-date-picker
            v-model="orderTimeDate"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button icon="el-icon-delete" type="text" size="mini" @click="rowDel(scope.row)">删除</el-button>
          <el-button
            type="text"
            icon="el-icon-view"
            size="mini"
            plain
            @click="jumpToDetail(scope.row)"
          >详情</el-button>
          <el-button
            type="text"
            size="mini"
            plain
            v-show="scope.row.reviewStatus !== '3'"
            @click="jumpToEdit(scope.row)"
          >编辑</el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-s-flag"
            v-show="scope.row.reviewStatus !== '2'"
            @click="jumpToReview(scope.row)"
          >审核</el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-view"
            v-show="scope.row.reviewStatus === '3'"
            @click="jumpToShip(scope.row)"
          >发货单</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, delObj } from "../../../api/crm/salesorder";
import { tableOption } from "@/const/crud/crm/salesorder";
import { mapGetters } from "vuex";

export default {
  name: "salesorder",
  data() {
    return {
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
        isAsc: false // 是否倒序
      },
      tableLoading: false,
      tableOption: tableOption,
      orderTimeDate: []
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.crm_salesorder_add, false),
        delBtn: this.vaildData(this.permissions.crm_salesorder_del, false),
        editBtn: this.vaildData(this.permissions.crm_salesorder_edit, false)
      };
    }
  },
  created() {
    this.getHeight();
  },
  methods: {
    getHeight() {
      const tableHeight = Math.max(window.innerHeight - 530, 200);
      this.tableOption.height = tableHeight + "px";
    },
    getList(page, params) {
      this.tableLoading = true;
      if (this.orderTimeDate.length > 1) {
        params = {
          ...params,
          startDate: this.orderTimeDate[0],
          endDate: this.orderTimeDate[1],
        };
      }
      console.log(params, page);
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params
        )
      ).then(res => {
        console.log(res);
        this.tableData = res.data.data.records;
        this.page.total = res.data.data.total;
        this.tableLoading = false;
      });
    },
    rowDel: function(row, index) {
      console.log(row);
      var _this = this;
      this.$confirm("是否确认删除ID为" + row.salesOrderSn, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          const params = {
            salesOrderSn: row.salesOrderSn
          };
          delObj(params).then(res => {
            console.log(res);
            if (res.data.code === 0) {
              _this.$message({
                showClose: true,
                message: "删除成功",
                type: "success"
              });
            }
            _this.getList(_this.page);
          });
        })
        .catch(function(err) {});
    },
    /**
     * 搜索回调
     */
    searchChange(form, done) {
      this.getList(this.page, form);
      done();
    },
    searchReset() {
      this.orderTimeDate = [];
    },
    /**
     * 刷新回调
     */
    refreshChange(info) {
      this.getList(this.page, this.filterForm(info.searchForm));
    },
    jumpToDetail(row) {
      let query = {
        res: {
          salesOrderSn: row.salesOrderSn
        }
      };
      console.log(query);
      
      this.$router.push({ path: "/orderCon/salesOrderDetail", query: query });
    },
    jumpToEdit(row) {
      let query = {
        res: {
          salesOrderSn: row.salesOrderSn
        }
      };
      this.$router.push({ path: "/orderCon/editOrder", query: query });
    },
    jumpToReview: function(row) {
      let query = {
        res: {
          salesOrderSn: row.salesOrderSn
        }
      };
      this.$router.push({ path: "/orderCon/salesOrderReview", query: query });
    },
    jumpToShip: function(row) {
      let query = {
        res: {
          orderSn: row.salesOrderSn
        }
      };
      this.$router.push({ path: "/shipRecord/shipList", query: query });
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
